# -*- coding: UTF-8 -*-
"""
消息栏
By AnpyD
"""

import bs
import random
import json
import information
from game_stats import DATA_PATH

stats_file = DATA_PATH + "/stats.json"

def get_players(bs):
    for player in bs.getSession().players:
        yield player

class Text(bs.Actor):
    def __init__(self):
        bs.Actor.__init__(self)

        self.key = 0

        self._type = 0 # 0 is informatino; 1 is player stats

        self.players = get_players(bs)

        self.text_timer = bs.Timer(6000, self.Text, repeat=True)

        try:
            with open(stats_file, 'r') as f:
                self._data = json.loads(f.read())
        except IOError:
            self._data = None

    def get_stats(self, stats):
        try:
            player = next(self.players)
            aid = player.get_account_id()
            if str(aid) in stats:
                player_stat = stats[str(aid)]
                text = str(player.getName(full=True).encode("utf-8")) + " 此赛季的排名：\n " + str(
                    player_stat["scores"]) + " 分 | " + str(
                    player_stat["kills"]) + " 次击杀 | " + str(
                    player_stat["deaths"]) + " 次死亡"
                return text
            else:
                return None
        except:
            return None

    def Text(self):
        try:
            if self._data is not None:
                self.stats_information = self.get_stats(self._data)
                if self._type == 0:
                    self.information = information.Information[self.key]
                    self._type = 1
                else:
                    if self.stats_information == None:
                        self.information = ""
                        self._type = 0
                    else:
                        self.information = self.stats_information
                        self._type = 0
                self.key += 1
            else:
                self.information = information.Information[self.key]
                self.key += 1

        except Exception as e:
            if self._data is not None:
                self.stats_information = self.get_stats(self._data)
                self.key = 0
                if self._type == 0:
                    self.information = information.Information[self.key]
                    self._type = 1
                else:
                    if self.stats_information == None:
                        self.information = ""
                        self._type = 0
                    else:
                        self.information = self.stats_information
                        self._type = 0
            else:
                self.key = 0
                self.information = information.Information[self.key]


        self.text = bs.newNode('text',
                                attrs={'text': self.information,
                                        'scale': 0.8,
                                        'maxWidth': 0,
                                        'position': (350, 650),
                                        'shadow': 0.5,
                                        'color': ((0 + random.random() * 1.0), 
                                                    (0 + random.random() * 1.0),
                                                    (0 + random.random() * 1.0)),
                                        'flatness': 0.5,
                                        'hAlign': 'center',
                                        'vAttach': 'bottom'})

        bs.animate(self.text, 'opacity', {0: 0.0, 1000: 1.0, 5000: 1.0, 6000: 0.0})
        bs.gameTimer(9000, self.text.delete)

    def handleMessage(self, msg):
        if isinstance(msg, bs.DieMessage):
            try:
                self.node.delete()
                self.text_timer = None
            except:
                pass
        else:
            bs.Actor.handleMessage(self, msg)

    def delete(self):
        self.handleMessage(bs.DieMessage())